package cn.edu.xaut.mybatisers.mall.Exception;

import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.*;
import cn.edu.xaut.mybatisers.mall.ServiceImp.BillServiceImp;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.mail.MailException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Map;


@ControllerAdvice
public class GlobalExceptionAdvice {

    private static Logger logger = Logger.getLogger(GlobalExceptionAdvice.class);

    @ApiOperation("捕获请求参数不完整的异常")
    @ExceptionHandler(ParametersIncompleteException.class)
    @ResponseBody
    public AReturnMessage handlerParamIncomplete(HttpServletRequest request, ParametersIncompleteException e) {
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setMessage("请求参数错误: [" + e.getParamNames() + "]");
        message.setStatusCode("500");
        return message;
    }

    @ApiOperation("捕获账号重复异常")
    @ExceptionHandler(TeleNumberExistException.class)
    @ResponseBody
    public AReturnMessage handlerTeleException(HttpServletRequest request, TeleNumberExistException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage(e.getMessage());
        return message;
    }

    @ApiOperation("捕获数据库操作异常")
    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public AReturnMessage handlerDataBaseException(HttpServletRequest request, SQLException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage("数据库操作错误");
        System.out.println(e.getClass());
        logger.error(
                "uri: " +  message.getUri() + "   " +
                "message" + message.getMessage() + "  "  +
                e.getMessage()
        );
        return  message;
    }

    @ApiOperation("捕获登录或登录失效的异常")
    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public AReturnMessage handlerLoginException(HttpServletRequest request, LoginException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage(e.getMessage());
        return message;
    }

    @ApiOperation("捕获发送邮箱验证码异常")
    @ExceptionHandler(MailException.class)
    @ResponseBody
    public AReturnMessage handlerMailException(HttpServletRequest request, MailException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage("邮件验证码发送失败");
        return message;
    }

    @ApiOperation("捕获不是会员异常")
    @ExceptionHandler(NotMemberException.class)
    @ResponseBody
    public AReturnMessage handlerMailException(HttpServletRequest request, NotMemberException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage(e.getMessage());
        return message;
    }

    @ApiOperation("捕获订单状态修改异常")
    @ExceptionHandler(BillStatusException.class)
    @ResponseBody
    public AReturnMessage handlerBillStatusException(HttpServletRequest request, BillStatusException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage(e.getMessage());
        return message;
    }

    @ApiOperation("捕获权限异常")
    @ExceptionHandler(AuthorityException.class)
    @ResponseBody
    public AReturnMessage handlerAuthorityException(HttpServletRequest request, AuthorityException e){
        AReturnMessage message = new AReturnMessage();
        message.setUri(request.getRequestURI());
        message.setMethod(request.getMethod());
        message.setStatusCode("500");
        message.setMessage(e.getMessage());
        return message;
    }
}
